package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Resource
    private EmpService empService;
    @Resource
    private StudentService studentService;

    @Override
    public void saveClazz(Clazz clazz) {
        if(clazz.getBeginDate().isAfter(clazz.getEndDate())){
           throw new RuntimeException("开始时间晚于结束时间");
        }

        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(clazz.getCreateTime());
        save(clazz);
    }

    @Override
    public Clazz getClazz(Integer id) {
        Clazz clazz = getById(id);
        clazz.setMasterName(empService.getById(clazz.getMasterId()).getName());
        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        updateById(clazz);
    }

    @Override
    public void deleteClazz(Integer id) {
        if(studentService.lambdaQuery().eq(Student::getClazzId,id).count()!=0){
            throw new RuntimeException("该班仍有学生存在");
        }
            removeById(id);
    }

    @Override
    public List<Clazz> listClazz() {
        return list();
    }

    @Override
    public PageResult<Clazz> pageClazz(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        //第一部分
        //分页
        Page<Clazz> clazzPage = Page.of(page, pageSize);
      /*  QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();

        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        if (begin != null) {
            queryWrapper.ge("create_time", begin);
        }
        if (end != null) {
            queryWrapper.le("create_time", end);
        }
        Page<Clazz> p = baseMapper.selectPage(clazzPage, queryWrapper);
*/
        //带条件
        Page<Clazz> query=lambdaQuery().like(name!=null&&!name.isEmpty(),Clazz::getName,name).
                ge(begin!=null,Clazz::getBeginDate, begin).le(end!=null,Clazz::getEndDate, end).page(clazzPage);

        //第二部分
        // 获取数据库的数据
        List<Clazz> clazzList = query.getRecords();
        LocalDate now = LocalDate.now();
        //设置状态
        clazzList.forEach(clazz -> {
            clazz.setStatus((now .isAfter(clazz.getBeginDate()) ) ? ((now.isAfter(clazz.getEndDate())) ? "已结课" : "已开课") : "未开课");
        });
        //第三部分
        //多表查询
        List<Integer> MasterIds = clazzList.stream().map(Clazz::getMasterId).toList();
        if(!MasterIds.isEmpty()){
            //获取数据库数据
            List<Emp> emps = empService.listByIds(MasterIds);
           /*clazzList.forEach(e->{
              emps.stream().filter(emp->emp.getId().equals(e.getMasterId())).findFirst().
                      ifPresent(emp ->e.setMasterName(emp.getName()) );
                   } );*/
            Map<Integer, String> empMap = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
            clazzList.forEach(clazz -> clazz.setMasterName(empMap.get(clazz.getMasterId())));
        }
        return new PageResult<>(query.getTotal(),clazzList);
    }
}
